package com.study.test.demo.observer.general;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author hupengming
 * @ClassName ConcreteSubject
 * @Date 2023-03-14  15:15
 * @Version 1.0
 */
public class ConcreteSubject<E> implements ISubject<E>{

    private List<IObserver<E>> observers = new ArrayList<>();

    @Override
    public boolean attach(IObserver observer) {
        return !this.observers.contains(observer) && this.observers.add(observer);
    }

    @Override
    public boolean detach(IObserver observer) {
        return this.observers.remove(observer);
    }

    @Override
    public void notify(E e) {
        for (IObserver<E> observer : this.observers) {
            observer.update(e);
        }
    }

}
